// pages/order/order.js
import { OrderService } from '../../services/order_service';
var currentPage = 1;
var totalPages = 1;
var totalResults = 0;
const limit = 1000;

Page({

  data: {
    orders: [],
  },

  onLoad: function (options) {
   this.getOrders(1)
  },

  async getOrders(page, needClear = false) {
    let orderService = new OrderService();
    orderService.getOrders({
      page: page,
      limit: limit,
      sortBy: "-createdAt"
    }).then(res => {
      currentPage = res.data.page;
      totalPages = res.data.totalPages;
      totalResults = res.data.totalResults;
      if (needClear) {
        this.setData({
          orders: res.data.results
        })
      } else {
        this.setData({
          orders: this.data.orders.concat(res.data.results)
        })
      }
    })
  },

  async onPullDownRefresh() {
    await this.getOrders(1, true);
    wx.stopPullDownRefresh();
  },

  goTake() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  goDetail(event) {
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?id=' + id,
    });
  },
})